package com.example.androidtvdemo.demo02;

import android.os.Bundle;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.HorizontalGridView;
import androidx.leanback.widget.ItemBridgeAdapter;
import androidx.leanback.widget.OnChildViewHolderSelectedListener;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidtvdemo.databinding.ActivityHorizontalBinding;


/**
 * Create by os on 2021/9/7
 * Desc : 水平grid view
 */
public class HorizontalActivity extends FragmentActivity {
    private ActivityHorizontalBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityHorizontalBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        HorizontalGridView hgv = mBinding.hgv;
        //水平间距
        hgv.setHorizontalSpacing(20);
        hgv.setOnChildViewHolderSelectedListener(new OnChildViewHolderSelectedListener() {
            @Override
            public void onChildViewHolderSelected(RecyclerView parent, RecyclerView.ViewHolder child, int position, int subposition) {
                super.onChildViewHolderSelected(parent, child, position, subposition);
                Log.i("->tv", "选中:" + position);
            }
        });

        HorizontalPresenter presenter = new HorizontalPresenter();
        ArrayObjectAdapter aoa = new ArrayObjectAdapter(presenter);
        ItemBridgeAdapter iba = new ItemBridgeAdapter(aoa);
        aoa.add("动漫");
        aoa.add("动`漫");
        aoa.add("动2漫");
        aoa.add("动3漫");
        aoa.add("动5漫");
        aoa.add("动漫4");
        aoa.add("动漫5");
        aoa.add("动漫234");
        aoa.add("动漫2r");
        aoa.add("动漫df");
        aoa.add("动漫as");
        aoa.add("动漫we");
        aoa.add("动漫sdsf");
        aoa.add("动漫fff");
        aoa.add("动漫weeeeee");
        hgv.setAdapter(iba);
    }
}